import axios from 'axios'
// import { Message } from 'element-ui'
import router from '@/router/index'

// 配置默认的host,
axios.defaults.baseURL = '/'// https://saas-test1.tlwok.com/'
axios.defaults.withCredentials = true // 跨域时携带cookie凭证。axios默认跨域请求不带cookie
// 添加请求拦截器
axios.interceptors.request.use(
  function (config) {
    return config
  },
  function (error) {
    return Promise.reject(error)
  },
)

// 添加响应拦截器
axios.interceptors.response.use(
  function (response) {
    // 对响应数据做点什么
    if (response.status === 200) {
      if (response.data.code === '00001') {
        // 代表未登录
        if (document.URL.indexOf('admin') != -1) {
          console.log('后台未登录')
          location.href = location.origin + '/#/admin/Login'
        } else {
          const url = encodeURIComponent(location.href)
          location.href = response.data.desc + url
          console.log('前台未登录')
        }
      } else if (response.data.code === '00003') {
        router.push('/404')
      } else {
        return response.data
      }
    } else {
      throw Error('网络出错')
    }
  },
  function (error) {
    // 对响应错误做点什么
    // loadingInstance.close()
    return Promise.reject(error)
  },
)
export default axios

// 创建两个请求实例，分别对应catalog和userCenter的请求
// const serviceAxios = axios.create({
//   baseURL: config.serviceHost
// })

// const systemAxios = axios.create({
//   baseURL: config.systemHost
// })
// export {serviceAxios, systemAxios}
